$(function() {
    var top = $('#j-goods-info-more').offset().top;
    /**
     * 固定条(商品信息)的显示/隐藏
     */
    (function() {
        var timer = null;
        $(window).on('scroll', function() {
            clearTimeout(timer);
            timer = setTimeout(function() {
                if (top <= $(window).scrollTop()) {
                    $('#j-fixed-info').stop().animate({
                        'top': 0,
                        'opacity': 1
                    }, 300);
                } else {
                    $('#j-fixed-info').stop().animate({
                        'top': '-50px',
                        'opacity': 0
                    }, 300);
                }
            }, 200);
        });
    })();
    /**
     * 商品tab&动画描点
    */
    (function() {
        $('.goods-tab-btn-1').on('click', function() {
            $('html, body').animate({
                'scrollTop': top + 'px'
            });
            $('.goods-tab-btn-1').addClass('btn-active').siblings('.btn-active').removeClass('btn-active');
            $('#j-goods-info-more-tab-content').children('li.content').hide();
            $('#j-goods-info-more-tab-content').children('li.content-1').show();
        });
        $('.goods-tab-btn-2').on('click', function() {
            $('html, body').animate({
                'scrollTop': top + 'px'
            });
            $('.goods-tab-btn-2').addClass('btn-active').siblings('.btn-active').removeClass('btn-active');
            $('#j-goods-info-more-tab-content').children('li.content').hide();
            $('#j-goods-info-more-tab-content').children('li.content-2').show();
        });
        $('.goods-tab-btn-3').on('click', function() {
            $('html, body').animate({
                'scrollTop': top + 'px'
            });
            $('.goods-tab-btn-3').addClass('btn-active').siblings('.btn-active').removeClass('btn-active');
            $('#j-goods-info-more-tab-content').children('li.content').hide();
            $('#j-goods-info-more-tab-content').children('li.content-3').show();
        });
        /**
         * 倒计时
         */
        (function() {
            var $goods_info = $('div.m-goodsinfo').find('div.goods-info-1');
            var cutedown_1 = parseInt($goods_info.data('cutedown-1'));
            var cutedown_2 = parseInt($goods_info.data('cutedown-2'));
            // 0: 活动未开始, 1: 活动进行中, 2: 活动已结束
            function changeStauts(cutedown_1, cutedown_2, $goods_info) {
                var btn_text, text, status;
                if (0 <= cutedown_1) {
                    // 活动未开始
                    var cutedown = cutedown_1;
                    btn_text = $goods_info.data('text-1') || '即将开始';
                    $goods_info.find('button.btn').attr('disabled',true).addClass('btn-disable').html(btn_text);
                    $('a.btn-buy').fadeOut(300);
                    $('#j-timer-text').html('距离活动开始');
                } else if (0 > cutedown_1 && 0 < cutedown_2) {
                    // 活动已经开始
                    status = 1;
                    var cutedown = cutedown_2;
                    btn_text = $goods_info.data('text-2') || '立即购买';
                    $goods_info.find('button.btn').attr('disabled',false).removeClass('btn-disable').html(btn_text);
                    $('a.btn-buy').fadeIn(300);
                    $('#j-timer-text').html('距离活动结束');
                } else {
                    status = 2;
                    var cutedown = 0;
                    btn_text = $goods_info.data('text-3') || '促销结束';;
                    $goods_info.find('button.btn').attr('disabled',true).addClass('btn-disable').html(btn_text);
                    $('a.btn-buy').fadeOut(300);
                }
                //var day = Math.floor(cutedown / 3600 / 24);
                var hour = Math.floor(cutedown / 3600);
                var minute = Math.floor(cutedown % 3600 / 60);
                var second = Math.floor(cutedown % 60);
                //day = 10 > day? '0' + day: day;
                hour = 10 > hour ? '0' + hour : hour;
                minute = 10 > minute ? '0' + minute : minute;
                second = 10 > second ? '0' + second : second;
                $goods_info.find('p.time>a').eq(0).html(text);
                //$goods_info.find('span.day').html(day);
                $goods_info.find('span.hour').html(hour);
                $goods_info.find('span.min').html(minute);
                $goods_info.find('span.sec').html(second);
            }
            if(0 < $goods_info.length) {
                setInterval(function() {
                    cutedown_1--;
                    cutedown_2--;
                    changeStauts(cutedown_1, cutedown_2, $goods_info);
                }, 1000);
            }
        })();
    })();

    /**
     * 二维码
    */
    (function() {

        $('#QRcode').on('click', function() {
            var follow = document.getElementById('QRcode');
            QRcode = $('#QRcode').find('img').attr('src');
            var d = dialog({
                align: 'top left',
                quickClose: true,
                content: '<img src="'+QRcode+'" width="150" height="150" />'
            });
            d.show(follow);
        });
    })();
    
    $(".js_btn").click(function(){
    	$(this).attr('disabled',true).addClass('btn-disable');
    	location.href = buy_url;
	});
    $("#btn-buy11").click(function(){
    	$(this).attr('disabled',true).addClass('btn-buy1');
    	location.href = buy_url;
	});
    
});
